package com.xtxb.app.rmq.util;

import org.springframework.stereotype.Component;

import java.io.*;

@Component
public class FileServer {
    public FileServer(){
        File server=new File(System.getProperty("user.dir") + File.separator + "server.data");
        File topic=new File(System.getProperty("user.dir") + File.separator + "topic.data");
        if(!server.exists()) {
            try {
                server.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(!topic.exists()) {
            try {
                topic.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void saveCache(String type,Object cache){

        try (ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(System.getProperty("user.dir") + File.separator + type+".data"))) {
            os.writeObject(cache);
            os.flush();
        }catch (Exception e){

        }

    }

    public Object loadCache(String type){
        Object obj=null;
        try (ObjectInputStream os = new ObjectInputStream(new FileInputStream(System.getProperty("user.dir") + File.separator + type+".data"))) {
           obj = os.readObject();
        }catch (Exception e){

        }
        return obj;
    }
}
